package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.ItemCatService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
*       分类管理
* */
@Controller
@RequestMapping("item")
public class ItemCatController {

    @Reference
    private ItemCatService itemCatService;

    @RequestMapping("list")
    public String list(){
        return "admin/item_cat";
    }


    @RequestMapping("itemCatList")
    @ResponseBody
    public Result itemCatList(@RequestParam("parentId")Long parentId){
        try{
            List<TbItemCat> catList = itemCatService.itemCatList(parentId);
            return Result.response(ResultEnum.SUCCESS).setData(catList);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(TbItemCat tbItemCat){
        try{
            Integer i = itemCatService.save(tbItemCat);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("updateById")
    @ResponseBody
    public Result updateById(@RequestParam("id")Long id){
        try{
            TbItemCat i = itemCatService.updateById(id);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbItemCat tbItemCat){
        try{
            Integer i = itemCatService.updateInfo(tbItemCat);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("deleteCarById")
    @ResponseBody
    public Result deleteCarById(@RequestParam("id[]")Long[] id){
        try{
            Integer i = itemCatService.deleteCarById(id);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

}
